<template>
  <div class="about-ball-management">
    <!-- 操作按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="openAddDialog">新增约球记录</el-button>

    <!-- 约球信息表格 -->
    <el-table :data="paginatedAboutBalls" style="width: 100%" stripe>
      <el-table-column label="编号" prop="id" width="150" />
      <el-table-column label="城市" prop="city" width="200" />
      <el-table-column label="邀约门店" prop="aboutball_stores" width="200" />
      <el-table-column label="球桌类型" prop="aboutball_table" width="200" />
      <el-table-column label="邀约类型" prop="aboutball_type" width="200" />
      <el-table-column label="开台时间" prop="aboutball_time" width="200" />
      <el-table-column label="备注" prop="aboutball_remark" width="200" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="text" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteAboutBall(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控制和每页条数选择 -->
    <div class="pagination-wrapper">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :total="aboutBalls.length"
          layout="total, prev, pager, next, jumper"
          @current-change="handlePageChange"
          class="pagination"
      />
      <el-select v-model="pageSize" class="page-size-select" @change="handlePageSizeChange" placeholder="每页显示条数">
        <el-option label="3条" :value="3"></el-option>
        <el-option label="5条" :value="5"></el-option>
        <el-option label="10条" :value="10"></el-option>
        <el-option label="20条" :value="20"></el-option>
      </el-select>
    </div>

    <!-- 新增/编辑约球记录弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="currentAboutBall" :rules="rules" ref="aboutBallForm" label-width="100px">
        <el-form-item label="编号" prop="id">
          <el-input v-model="currentAboutBall.id" disabled placeholder="自动生成" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="currentAboutBall.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="邀约门店" prop="aboutball_stores">
          <el-input v-model="currentAboutBall.aboutball_stores" placeholder="请输入邀约门店" />
        </el-form-item>
        <el-form-item label="球桌类型" prop="aboutball_table">
          <el-input v-model="currentAboutBall.aboutball_table" placeholder="请输入球桌类型" />
        </el-form-item>
        <el-form-item label="邀约类型" prop="aboutball_type">
          <el-select v-model="currentAboutBall.aboutball_type" placeholder="请选择邀约类型">
            <el-option label="普通" value="normal"></el-option>
            <el-option label="高级" value="advanced"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开台时间" prop="aboutball_time">
          <el-input v-model="currentAboutBall.aboutball_time" placeholder="请输入开台时间" />
        </el-form-item>
        <el-form-item label="备注" prop="aboutball_remark">
          <el-input v-model="currentAboutBall.aboutball_remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAboutBall">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      aboutBalls: [
        { id: 1, city: '北京', aboutball_stores: '门店A', aboutball_table: '台桌1', aboutball_type: 'normal', aboutball_time: '2024-11-01 10:00', aboutball_remark: '备注1' },
        { id: 2, city: '上海', aboutball_stores: '门店B', aboutball_table: '台桌2', aboutball_type: 'advanced', aboutball_time: '2024-11-02 14:00', aboutball_remark: '备注2' },
        { id: 3, city: '广州', aboutball_stores: '门店C', aboutball_table: '台桌3', aboutball_type: 'normal', aboutball_time: '2024-11-03 18:00', aboutball_remark: '备注3' }
      ],
      currentAboutBall: { id: null, city: '', aboutball_stores: '', aboutball_table: '', aboutball_type: '', aboutball_time: '', aboutball_remark: '' },
      dialogVisible: false,
      dialogTitle: '新增约球记录',
      currentPage: 1,
      pageSize: 3,
      rules: {
        city: [{ required: true, message: '请输入城市', trigger: 'blur' }],
        aboutball_stores: [{ required: true, message: '请输入邀约门店', trigger: 'blur' }],
        aboutball_table: [{ required: true, message: '请输入球桌类型', trigger: 'blur' }],
        aboutball_type: [{ required: true, message: '请选择邀约类型', trigger: 'change' }],
        aboutball_time: [{ required: true, message: '请输入开台时间', trigger: 'blur' }],
        aboutball_remark: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      }
    };
  },
  computed: {
    paginatedAboutBalls() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = this.currentPage * this.pageSize;
      return this.aboutBalls.slice(start, end);
    }
  },
  methods: {
    openAddDialog() {
      this.currentAboutBall = { id: null, city: '', aboutball_stores: '', aboutball_table: '', aboutball_type: '', aboutball_time: '', aboutball_remark: '' };
      this.dialogTitle = '新增约球记录';
      this.dialogVisible = true;
    },
    openEditDialog(aboutBall) {
      this.currentAboutBall = { ...aboutBall };
      this.dialogTitle = '编辑约球记录';
      this.dialogVisible = true;
    },
    saveAboutBall() {
      this.$refs.aboutBallForm.validate((valid) => {
        if (valid) {
          if (this.currentAboutBall.id) {
            const index = this.aboutBalls.findIndex(item => item.id === this.currentAboutBall.id);
            this.$set(this.aboutBalls, index, this.currentAboutBall);
          } else {
            this.currentAboutBall.id = this.aboutBalls.length + 1;
            this.aboutBalls.push(this.currentAboutBall);
          }
          this.dialogVisible = false;
          this.$message.success(`${this.dialogTitle === '新增约球记录' ? '新增' : '编辑'}成功`);
        }
      });
    },
    deleteAboutBall(aboutBall) {
      this.$confirm('确认删除该约球记录吗?', '删除约球记录', { type: 'warning' }).then(() => {
        this.aboutBalls = this.aboutBalls.filter(item => item.id !== aboutBall.id);
        this.$message.success('删除成功');
      }).catch(() => {});
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
    handlePageSizeChange(size) {
      this.pageSize = size;
    }
  }
};
</script>

<style scoped>
.about-ball-management {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}

.el-input[type="textarea"] {
  resize: none;
}

.pagination-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

.page-size-select {
  margin-left: 10px;
  width: 150px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
